package com.internetCafes.spms.web.customer.service.impl.user;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.web.customer.common.util.UserInfoUtil;
import com.internetCafes.spms.web.customer.entity.user.EntUserInfo;
import com.internetCafes.spms.web.customer.mapper.user.EntUserInfoMapper;
import com.internetCafes.spms.web.customer.model.user.*;
import com.internetCafes.spms.web.customer.service.user.IEntUserInfoService;
import com.internetCafes.spms.web.sys.model.UserInfo;
import com.internetCafes.spms.web.tenant.model.podo.TenantDeptInfoPlusDo;
import com.internetCafes.spms.web.tenant.model.podo.TenantDeptInfoPlusPo;
import com.internetCafes.spms.web.tenant.service.ITenantDeptInfoPlusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntUserInfoServiceImpl extends ServiceImpl<EntUserInfoMapper, EntUserInfo> implements IEntUserInfoService {

    @Autowired
    private ITenantDeptInfoPlusService deptInfoPlusService;

    @Override
    public boolean isLowerLevelUserIdListOfManagerUser(List<Long> userId, UserInfo userInfo) {
        if (UserInfoUtil.entSuperAdminJudge(userInfo)) {
            return true;
        }
        // 非管理员
        if (!UserInfoUtil.managerJudge(userInfo)) {
            return false;
        }
        // 根据当前用户信息 找到子部门以及子部门以下的人员id
        List<Long> deptIdList = deptInfoPlusService.deptList(new TenantDeptInfoPlusDo().setAncestorId(userInfo.getTenantDeptId()))
                .stream().map(TenantDeptInfoPlusPo::getId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(deptIdList)) {
            return false;
        }
        List<Long> lowerLevelUserIdList = userPoList(new UserDo().setDeptIdList(deptIdList))
                .stream()
                .map(UserPo::getId)
                .collect(Collectors.toList());
        return lowerLevelUserIdList.containsAll(userId);
    }

    @Override
    public boolean isLowerLevelUserIdOfManagerUser(Long userId, UserInfo userInfo) {
        if (UserInfoUtil.entSuperAdminJudge(userInfo)) {
            return true;
        }
        // 非管理员
        if (!UserInfoUtil.managerJudge(userInfo)) {
            return false;
        }
        // 获取人员的上级部门id列表
        List<Long> deptId = deptInfoPlusService.deptList(new TenantDeptInfoPlusDo().setChildEmployeeUserId(userId))
                .stream()
                .map(TenantDeptInfoPlusPo::getId)
                .collect(Collectors.toList());
        return !deptId.contains(userInfo.getTenantDeptId());
    }

    @Override
    public List<EntUserInfoPo> listPoNoTenantId(EntUserInfoDo entUserInfoDo) {
        return baseMapper.listPoNoTenantId(entUserInfoDo);
    }

    /**
     * 统计用户扩展信息
     *
     * @param userIdList 用户id列表
     */
    @Override
    public Integer countUserExtendInfoByIds(List<Long> userIdList) {
        return baseMapper.countUserExtendInfoByIds(userIdList);
    }

    @Override
    public List<UserPo> userPoList(UserDo userDo) {
        return baseMapper.userPoList(userDo);
    }

    @Override
    public List<EntUserEmployeeInfoListPo> userEmployeeInfoListPo(EntUserEmployeeInfoListDo listDo) {
        listDo.setTenantId(ShiroUtils.getUserEntity().getDeptId());
        return baseMapper.userEmployeeInfoListPo(listDo);
    }

    @Override
    public List<EntUserDetailListPo> userDetailList(EntUserDetailListDo listDo) {
        listDo.setDeptId(ShiroUtils.getUserEntity().getDeptId());
        if (UserInfoUtil.managerJudge(ShiroUtils.getUserEntity())) {
            listDo.setUserId(ShiroUtils.getUserId());
        }
        return baseMapper.userDetailList(listDo);
    }

    @Override
    public List<EntUserInfo> saleListByAreaId(Long areaId) {
        return baseMapper.saleListByAreaId(areaId);
    }

    @Override
    public Page<EntUserInfoPo> poPaging(Page<EntUserInfo> page, UserInfoDo userInfoDo) {
        return baseMapper.poPaging(page, userInfoDo);
    }

    @Override
    public List<EntUserInfoPo> poList(Wrapper<EntUserInfo> queryWrapper) {
        return baseMapper.poList(queryWrapper);
    }
}
